package ServiceImp;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import DAO.BrandDAO;
import DAO.CategoryDAO;
import DAO.CustomerTypeDAO;
import DAO.ImageDAO;
import DAO.ProductDAO;
import DAO.ProductDetailDAO;
import DAO.StoreDAO;
import DAO.StoreProductDetailDAO;
import DAO.TypeDAO;
import DAO.TypeDetailDAO;
import DAO.UserDAO;
import DAO.UserRoleDAO;
import POJO.Brand;
import POJO.Category;
import POJO.CustomerType;
import POJO.Image;
import POJO.Product;
import POJO.ProductDetail;
import POJO.Store;
import POJO.StoreProductDetail;
import POJO.Type;
import POJO.TypeDetail;
import POJO.User;
import POJO.UserRole;

@Service("createData")
@Transactional
public class CreateData {
	@Autowired
	ImageDAO imageDao;

	@Autowired
	BrandDAO brandDao;
	
	@Autowired
	CategoryDAO categoryDao;
	
	@Autowired
	TypeDAO typeDao;
	
	@Autowired
	TypeDetailDAO typeDetailDao;
	
	@Autowired
	UserDAO userDao;
	
	@Autowired
	ProductDAO productDao;
	
	@Autowired
	StoreDAO storeDao;
	
	@Autowired
	ProductDetailDAO productDetailDao;
	
	@Autowired
	StoreProductDetailDAO storeProductDetailDao;
	
	@Autowired
	UserRoleDAO userRoleDao;
	
	@Autowired
	CustomerTypeDAO customerTypeDao;
	
	public void generateData(){
		createImage();
		
		createBrand();
		
		createTypeTypeDetail();
		
		createUserTypeUser();
		
		createProduct();
	}

	private void createImage() {
		//imageDao.deleteAll();
		
		imageDao.create(new Image("resources/images/asr1.jpg"));
		imageDao.create(new Image("resources/images/asr2.jpg"));
		imageDao.create(new Image("resources/images/asr3.jpg"));
		imageDao.create(new Image("resources/images/asr4.jpg"));
		imageDao.create(new Image("resources/images/asr5.jpg"));
		imageDao.create(new Image("resources/images/asr6.jpg"));
		imageDao.create(new Image("resources/images/asr7.jpg"));
		imageDao.create(new Image("resources/images/asr8.jpg"));
		
		imageDao.create(new Image("resources/images/roma1.jpg"));
		imageDao.create(new Image("resources/images/roma2.jpg"));
		imageDao.create(new Image("resources/images/roma3.jpg"));
		imageDao.create(new Image("resources/images/roma4.jpg"));
		imageDao.create(new Image("resources/images/roma5.jpg"));
		imageDao.create(new Image("resources/images/roma6.jpg"));
		imageDao.create(new Image("resources/images/roma7.jpg"));
		imageDao.create(new Image("resources/images/roma8.jpg"));
		imageDao.create(new Image("resources/images/roma9.jpg"));
		imageDao.create(new Image("resources/images/roma10.jpg"));
		imageDao.create(new Image("resources/images/roma11.jpg"));
		
		imageDao.create(new Image("resources/images/barca1.jpg"));
		imageDao.create(new Image("resources/images/barca2.jpg"));
		imageDao.create(new Image("resources/images/barca3.jpg"));
		imageDao.create(new Image("resources/images/barca4.jpg"));
		imageDao.create(new Image("resources/images/barca5.jpg"));
		imageDao.create(new Image("resources/images/barca6.jpg"));
		imageDao.create(new Image("resources/images/barca7.jpg"));
		imageDao.create(new Image("resources/images/barca8.jpg"));
		imageDao.create(new Image("resources/images/barca9.jpg"));
		imageDao.create(new Image("resources/images/barca10.jpg"));
		imageDao.create(new Image("resources/images/barca11.jpg"));
		imageDao.create(new Image("resources/images/barca12.jpg"));
		imageDao.create(new Image("resources/images/barca13.jpg"));
	}
	
	private void createBrand() {
		
		//brandDao.deleteAll();
		
		brandDao.create(new Brand("Adidas"));
		brandDao.create(new Brand("Nike"));
		brandDao.create(new Brand("Việt Tiến"));
		
		//categoryDao.deleteAll();
	}
	
	private void createTypeTypeDetail() {
		int idSize = typeDao.create(new Type("Size"));
		int idColor = typeDao.create(new Type("Color"));
		
		typeDetailDao.create(new TypeDetail("S", typeDao.get(idSize)));
		typeDetailDao.create(new TypeDetail("M", typeDao.get(idSize)));
		typeDetailDao.create(new TypeDetail("L", typeDao.get(idSize)));
		typeDetailDao.create(new TypeDetail("XL", typeDao.get(idSize)));

		typeDetailDao.create(new TypeDetail("Xanh Da Trời", typeDao.get(idColor)));
		typeDetailDao.create(new TypeDetail("Đỏ", typeDao.get(idColor)));
		typeDetailDao.create(new TypeDetail("Xanh Lá Cây", typeDao.get(idColor)));
		typeDetailDao.create(new TypeDetail("Vàng", typeDao.get(idColor)));
		typeDetailDao.create(new TypeDetail("Đen", typeDao.get(idColor)));
		typeDetailDao.create(new TypeDetail("Trắng", typeDao.get(idColor)));
	}
	
	private void createUserTypeUser() {
		userDao.create(new User("admin", "123456"));
		
		userRoleDao.create(new UserRole(userDao.findUserByUserName("admin"), "ROLE_ADMIN"));
		
		customerTypeDao.create(new CustomerType("VIP_1"));
		customerTypeDao.create(new CustomerType("VIP_2"));
		customerTypeDao.create(new CustomerType("VIP_3"));
	}
	
	private void createProduct() {
		int idStore1 = storeDao.create(new Store("Store 1", "Store1 Address"));
		int idStore2 = storeDao.create(new Store("Store 2", "Store2 Address"));
		int idStore3 = storeDao.create(new Store("Store 3", "Store3 Address"));
		
		Set<Category> categoryArs = new HashSet<Category>(3);
		categoryArs.add(categoryDao.get(1));
		categoryArs.add(categoryDao.get(4));
		categoryArs.add(categoryDao.get(10));
		
		Set<Category> categoryRoma = new HashSet<Category>(3);
		categoryRoma.add(categoryDao.get(1));
		categoryRoma.add(categoryDao.get(4));
		categoryRoma.add(categoryDao.get(11));
		
		Set<Category> categoryBarca = new HashSet<Category>(3);
		categoryBarca.add(categoryDao.get(1));
		categoryBarca.add(categoryDao.get(4));
		categoryBarca.add(categoryDao.get(12));

		Date dateNow = new Date();
		Date dateOld = new Date(2014-1900, 10, 13);
		Date dateOld2 = new Date(2013-1900, 10, 10);
		
		int idProduct11 = productDao.create(
				new Product("Áo Arsenal Sân Nhà", "Sân Nhà, Arsenal, Áo", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(1), categoryArs, dateNow));
		int idProduct12 = productDao.create(
				new Product("Áo Arsenal Sân Khách", "Sân Khách, Arsenal, Áo", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(1), categoryArs, dateOld));
		int idProduct13 = productDao.create(
				new Product("Áo Arsenal Sân Khách (mẫu 2)", "Sân Khách, Arsenal, Áo, 2", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(1), categoryArs, dateOld2));
		
		
		int idProduct21 = productDao.create(
				new Product("Áo Roma Sân Nhà", "Sân Nhà, Roma, Áo", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(1), categoryRoma, dateOld));
		int idProduct22 = productDao.create(
				new Product("Áo Roma Sân Khách", "Sân Khách, Roma, Áo", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(1), categoryRoma, dateOld2));
		int idProduct23 = productDao.create(
				new Product("Áo Roma Sân Khách (mẫu 2)", "Sân Khách, Roma, Áo, 2", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(1), categoryRoma,dateNow));
		
	
		int idProduct31 = productDao.create(
				new Product("Áo Barcelona Sân Nhà", "Sân Nhà, Barcelona, Áo", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(2), categoryBarca, dateOld2));
		int idProduct32 = productDao.create(
				new Product("Áo Barcelona Sân Khách", "Sân Khách, Barcelona, Áo", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(2), categoryBarca, dateNow));
		int idProduct33 = productDao.create(
				new Product("Áo Barcelona Sân Khách (mẫu 2)", "Sân Khách, Barcelona, Áo, 2", "Áo Arsenal Sân Nhà Desc", "Áo Arsenal Sân Nhà Detail_Info", "Con_Hang", 0, brandDao.get(2), categoryBarca, dateNow));
		
		
		Set<Product> productAsr = new HashSet<Product>();
		productAsr.add(productDao.get(idProduct11));
		productAsr.add(productDao.get(idProduct12));
		productAsr.add(productDao.get(idProduct13));
		
		Set<Product> productRoma = new HashSet<Product>();
		productRoma.add(productDao.get(idProduct21));
		productRoma.add(productDao.get(idProduct22));
		productRoma.add(productDao.get(idProduct23));
		
		Set<Product> productBarca = new HashSet<Product>();
		productBarca.add(productDao.get(idProduct31));
		productBarca.add(productDao.get(idProduct32));
		productBarca.add(productDao.get(idProduct33));
		
		Set<Product> products = new HashSet<Product>();
		products.addAll(productAsr);
		products.addAll(productRoma);
		products.addAll(productBarca);
		
		Set<Image> imageArs = new HashSet<Image>(imageDao.getImages("asr"));
		Set<Image> imageRoma = new HashSet<Image>(imageDao.getImages("roma"));
		Set<Image> imageBarca = new HashSet<Image>(imageDao.getImages("barca"));
		
		int idParent01 = categoryDao.create(new Category("Áo Bóng Đá", 0, -1, imageBarca, products));
		int idParent02 = categoryDao.create(new Category("Áo Nhóm, Lớp", 0, -1, imageArs));
		int idParent03 = categoryDao.create(new Category("Giày Dép", 0, -1, imageRoma));
		
		int idParent11 = categoryDao.create(new Category("Áo Câu Lạc Bộ", 1, idParent01, imageBarca, products));
		int idParent12 = categoryDao.create(new Category("Áo Đội Tuyển", 1, idParent01, imageArs));

		int idParent13 = categoryDao.create(new Category("Áo Cổ Tròn", 1, idParent02, imageBarca));
		int idParent14 = categoryDao.create(new Category("Áo Trụ", 1, idParent02, imageArs));
		
		int idParent15 = categoryDao.create(new Category("Giày", 1, idParent03, imageBarca));
		int idParent16 = categoryDao.create(new Category("Dép", 1, idParent03, imageArs));
		
		
		int idParent21 = categoryDao.create(new Category("Áo Arsenal", 2, idParent11, imageBarca, productAsr));
		int idParent22 = categoryDao.create(new Category("Áo AS Roma", 2, idParent11, imageArs, productRoma));
		int idParent23 = categoryDao.create(new Category("Áo Barcelona", 2, idParent11, imageBarca, productBarca));
		
		int idParent24 = categoryDao.create(new Category("Áo Việt Nam", 2, idParent12, imageBarca));
		int idParent25 = categoryDao.create(new Category("Áo Anh", 2, idParent12, imageArs));
		int idParent26 = categoryDao.create(new Category("Áo Tây Ban Nha", 2, idParent12, imageBarca));
		
		Set<Image> asrSanNha = new HashSet<Image>(imageDao.getImages("asr").subList(0, 2 +1));
		Set<Image> asrSanKhach = new HashSet<Image>(imageDao.getImages("asr").subList(3, 5 +1));
		Set<Image> asrSanKhach2 = new HashSet<Image>(imageDao.getImages("asr").subList(6, 7 +1));
		
		Set<TypeDetail> sizeS = new HashSet<TypeDetail>();
		sizeS.add(typeDetailDao.get(1));
		sizeS.add(typeDetailDao.get(9));
		
		Set<TypeDetail> sizeM = new HashSet<TypeDetail>();
		sizeM.add(typeDetailDao.get(2));
		Set<TypeDetail> sizeL = new HashSet<TypeDetail>();
		sizeL.add(typeDetailDao.get(3));
		Set<TypeDetail> sizeXL = new HashSet<TypeDetail>();
		sizeXL.add(typeDetailDao.get(4));
		
		int idProductDetail11 = productDetailDao.create(new ProductDetail(0, 120000, 120000, 0, productDao.get(idProduct11), sizeM, asrSanNha, imageDao.get(1)));
		int idProductDetail12 = productDetailDao.create(new ProductDetail(0, 120000, 120000, 0, productDao.get(idProduct12), sizeM, asrSanKhach, imageDao.get(4)));
		int idProductDetail13 = productDetailDao.create(new ProductDetail(0, 130000, 130000, 0, productDao.get(idProduct13), sizeM, asrSanKhach2, imageDao.get(7)));
		int idProductDetail14 = productDetailDao.create(new ProductDetail(0, 100000, 130000, 0, productDao.get(idProduct13), sizeL, asrSanKhach2, imageDao.get(7)));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail11), 10));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail11), 20));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail11), 10));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail12), 15));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail12), 25));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail12), 15));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail13), 16));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail13), 26));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail13), 16));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail14), 36));
		
		Set<Image> romaSanNha = new HashSet<Image>(imageDao.getImages("roma").subList(0, 2));
		Set<Image> romaSanKhach = new HashSet<Image>(imageDao.getImages("roma").subList(2, 6 +1));
		Set<Image> romaSanKhach2 = new HashSet<Image>(imageDao.getImages("roma").subList(7, 9 +1));
		
		int idProductDetail21 = productDetailDao.create(new ProductDetail(0, 100000, 100000, 0, productDao.get(idProduct21), sizeS, romaSanNha, imageDao.get(19)));
		int idProductDetail22 = productDetailDao.create(new ProductDetail(0, 120000, 150000, 0, productDao.get(idProduct22), sizeL, romaSanKhach, imageDao.get(11)));
		int idProductDetail23 = productDetailDao.create(new ProductDetail(0, 100000, 120000, 0, productDao.get(idProduct23), sizeS, romaSanKhach2, imageDao.get(17)));
		int idProductDetail24 = productDetailDao.create(new ProductDetail(0, 120000, 120000, 0, productDao.get(idProduct23), sizeM, romaSanKhach2, imageDao.get(17)));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail21), 10));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail21), 20));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail21), 10));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail22), 15));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail22), 25));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail22), 15));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail23), 16));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail23), 26));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail23), 16));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail24), 20));
		
		Set<Image> barcaSanNha = new HashSet<Image>(imageDao.getImages("barca").subList(6, 8 +1));
		Set<Image> barcaanKhach = new HashSet<Image>(imageDao.getImages("barca").subList(1, 5 +1));
		Set<Image> barcaanKhach2 = new HashSet<Image>(imageDao.getImages("barca").subList(9, 12 +1));
		
		int idProductDetail31 = productDetailDao.create(new ProductDetail(0, 100000, 200000, 0, productDao.get(idProduct31), sizeS, barcaSanNha, imageDao.get(26)));
		int idProductDetail32 = productDetailDao.create(new ProductDetail(0, 100000, 200000, 0, productDao.get(idProduct32), sizeM, barcaanKhach, imageDao.get(21)));
		int idProductDetail33 = productDetailDao.create(new ProductDetail(0, 100000, 120000, 0, productDao.get(idProduct33), sizeL, barcaanKhach2, imageDao.get(29)));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail31), 10));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail31), 20));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail31), 10));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail32), 15));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail32), 25));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail32), 15));
		
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore1), productDetailDao.get(idProductDetail33), 16));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore2), productDetailDao.get(idProductDetail33), 26));
		storeProductDetailDao.create(new StoreProductDetail(storeDao.get(idStore3), productDetailDao.get(idProductDetail33), 16));
	}
}















